package com.coco.seck.service.impl;

import com.alibaba.fastjson.JSON;
import com.coco.common.result.ResultData;
import com.coco.seck.vo.DeployVo;
import com.coco.seck.mapper.DeployMapper;
import com.coco.seck.pojo.TbDeploy;
import com.coco.seck.service.DeployService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import springfox.documentation.spring.web.json.Json;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author HanSama
 * @date 2021年07月15日 22:33
 */
@Service
public class DeployServiceImpl implements DeployService {

    @Autowired
    private DeployMapper deployMapper;



    /**
     * 查询全部包含逻辑删除内容
     * @author HanSama
     * @date 2021/7/18 10:05
     * @return java.util.List<com.coco.seck.pojo.TbDeploy>
     */
    @Override
    public List<TbDeploy> findAll() {
        return deployMapper.findAll();
    }

    /**
     * 条件查询分页
     * @author HanSama
     * @date 2021/7/18 10:04
     * @param deployVo
     * @return com.coco.common.result.ResultData
     */
    @Override
    public ResultData findDeployList(DeployVo deployVo) {
        PageHelper.startPage(deployVo.getPage(),deployVo.getLimit());
        List<TbDeploy> deployList = deployMapper.findDeployList(deployVo);
        PageInfo<TbDeploy> pageInfo = new PageInfo<>(deployList);
        return new ResultData(pageInfo.getList(),pageInfo.getTotal());
    }

    /**
     * 添加秒杀配置
     * @author HanSama
     * @date 2021/7/18 10:05
     * @param deploy
     */
    @Override
    public void addDeploy(TbDeploy deploy) {
        List<String> imgList = deploy.getImgList();
        if (imgList==null || imgList.size()<1){
            imgList = new ArrayList<>(2);
        }
        String s = JSON.toJSONString(imgList);
        deploy.setDeployImg(s);
        deploy.setCreateTime(new Date());
        deployMapper.addDeploy(deploy);
    }

    /**
     * 修改秒杀配置
     * @author HanSama
     * @date 2021/7/18 10:05
     * @param deploy
     */
    @Override
    public void updateDeploy(TbDeploy deploy) {
        List<String> imgList = deploy.getImgList();
        String s = JSON.toJSONString(imgList);
        deploy.setDeployImg(s);
        deployMapper.updateDeploy(deploy);
    }

    /**
     * 逻辑删除秒杀配置
     * @author HanSama
     * @date 2021/7/18 10:05
     * @param id
     */
    @Override
    public void deleteById(Long id) {
        deployMapper.deleteById(id);
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        deployMapper.updateStatus(id,status);
    }

    @Override
    public TbDeploy selectById(Long id) {
        return deployMapper.selectById(id);
    }


}
